package com.lry.jvm.rtda;

import com.lry.jvm.util.Util;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Stack {

    private int maxSize;
    private int size;
    private Frame top;

    public Stack(int maxStack) {
        this.maxSize = maxStack;
    }

    //压栈，压一个帧
    public void push(Frame frame) {
        if(size>=maxSize){
            Util.panic("java.lang.StackOverflowError");
        }

        if(top!=null){
            frame.next = top;
        }
        top = frame;
        size++;
    }

    public Frame pop() {
        if(null==top){
            Util.panic("jvm stack is empty");
        }
        Frame res = top;
        top = top.next;
        size--;
        return res;
    }

    public Frame peek() {
        if(null==top){
            Util.panic("jvm stack is empty");
        }
        return top;
    }

    public boolean isStackEmpty() {
        return size==0;
    }
}
